package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.vo.ZsyyGetcashVo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyParkSpaceAddBo;
import com.ruoyi.system.bo.ZsyyParkSpaceQueryBo;
import com.ruoyi.system.bo.ZsyyParkSpaceEditBo;
import com.ruoyi.system.domain.ZsyyParkSpace;
import com.ruoyi.system.mapper.ZsyyParkSpaceMapper;
import com.ruoyi.system.vo.ZsyyParkSpaceVo;
import com.ruoyi.system.service.IZsyyParkSpaceService;
import com.ruoyi.system.mapper.ZsyyParkSpaceMapper;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 车位信息Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyParkSpaceServiceImpl extends ServicePlusImpl<ZsyyParkSpaceMapper, ZsyyParkSpace> implements IZsyyParkSpaceService {
    @Autowired
    private ZsyyParkSpaceMapper iZsyyParkSpaceMapper;

    @Override
    public ZsyyParkSpaceVo queryById(Long id){
        return getVoById(id, ZsyyParkSpaceVo.class);
    }

    @Override
    public TableDataInfo<ZsyyParkSpaceVo> queryPageList(ZsyyParkSpaceQueryBo bo) {
		Page<ZsyyParkSpaceVo> page = iZsyyParkSpaceMapper.queryPageList(PageUtils.buildPage(),bo);
		return PageUtils.buildDataInfo(page);
    }

    @Override
    public List<ZsyyParkSpaceVo> queryList(ZsyyParkSpaceQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyParkSpaceVo.class);
    }

    private LambdaQueryWrapper<ZsyyParkSpace> buildQueryWrapper(ZsyyParkSpaceQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyParkSpace> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getSpaceNum()), ZsyyParkSpace::getSpaceNum, bo.getSpaceNum());
        lqw.eq(bo.getParkId() != null, ZsyyParkSpace::getParkId, bo.getParkId());
        lqw.eq(bo.getType() != null, ZsyyParkSpace::getType, bo.getType());
        lqw.eq(StrUtil.isNotBlank(bo.getCarCode()), ZsyyParkSpace::getCarCode, bo.getCarCode());
        lqw.eq(StrUtil.isNotBlank(bo.getCarUser()), ZsyyParkSpace::getCarUser, bo.getCarUser());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyParkSpaceAddBo bo) {
    	bo.setCreatetime(new Date());
        ZsyyParkSpace add = BeanUtil.toBean(bo, ZsyyParkSpace.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyParkSpaceEditBo bo) {
    	bo.setUpdatetime(new Date());
        ZsyyParkSpace update = BeanUtil.toBean(bo, ZsyyParkSpace.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyParkSpace entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
